package com.dycx.system.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.system.dto.PermissionDTO;
import com.dycx.system.entity.PermissionDO;
import com.dycx.system.vo.PermissionVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限表(Action)实体转换类
 *
 * @author wuzongbing
 * @since 2025-02-04 15:06:01
 */


@Data
public class PermissionConverter implements Serializable {
    public static PermissionDO dtoToEntity(PermissionDTO dto) {
        if (dto == null) return null;
        PermissionDO entity = BeanUtil.toBean(dto, PermissionDO.class);
        return entity;
    }

    public static PermissionVO entityToVo(PermissionDO entity) {
        if (entity == null) return null;
        PermissionVO vo = BeanUtil.toBean(entity, PermissionVO.class);

        return vo;
    }

    public static List<PermissionVO> entityToVoList(List<PermissionDO> entityList) {
        List<PermissionVO> voList = new ArrayList<>();
        for (PermissionDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<PermissionVO> entityToVoPage(Page<PermissionDO> page) {

        List<PermissionVO> newRecords = new ArrayList<>();

        List<PermissionDO> records = page.getRecords();
        for (PermissionDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<PermissionVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
